package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {
    /**分页查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDTO dto) {
        //判断参数是否可用
        if (dto==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页参数检查
        dto.checkParam();
        //创建mp的查询对象
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        //判断传入name是否为空
        if (StringUtils.isNoneBlank(dto.getName())) {
            queryWrapper.like(AdChannel::getName,dto.getName());
        }
        //创建分页对象 传入参数
        Page page = new Page<AdChannel>(dto.getPage(), dto.getSize());

        //排序
        queryWrapper.orderByAsc(AdChannel::getOrd);
        //封装分页对象
        IPage result = page(page, queryWrapper);

        //结果封装
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), result.getTotal(),result.getRecords());

        return responseResult;
    }


    /**添加
     *
     * @param channel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel channel) {
        //1.校验参数 频道名称不为空 不能大于10个字符
        String name = channel.getName();
        if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能为空");
        }
        //名字长度不能超过十个字
        if(name.length()>10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能大于10个字符");
        }
        //不能重复
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, channel.getName()));
        if(count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名称已经存在");
        }
        //添加时间
        channel.setCreatedTime(new Date());
        //2.保存频道
        save(channel);
        //3.返回结果
        return ResponseResult.okResult();
    }

    /**修改
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1 参数校验
        if(adChannel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"id不能为空");
        }
        //2 执行修改
        AdChannel channel = this.getById(adChannel.getId());
        //判断是否有这个频道
        if (channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST);
        }
        //3. 校验名称唯一性
        if (StringUtils.isNoneBlank(adChannel.getName())&&!channel.getName().equals(adChannel.getName())) {
            int count = this.count(Wrappers.<AdChannel>lambdaQuery()
                    .eq(AdChannel::getName, adChannel.getName()));
            //判断频道名称是否存在
            if(count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名称已经存在");
            }
        }
        //执行修改方法
        this.updateById(adChannel);
        //4 返回结果
        return ResponseResult.okResult();
    }

    /**根据id删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        //1.检查参数
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id不能为空");
        }
        //2.判断当前频道是否存在 和 是否有效
        AdChannel channel = this.getById(id);
        if(channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 启用状态下不能删除
        if (channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"启用状态不能删除");
        }
        //3.删除频道
        this.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
